import parentModel from "../parentModel";
export default class routingModel extends parentModel {
    constructor() {
        super(routingModel,util.api.RoutingModel);
        this.table='routingTemplates';
        this.typeList=['routing'];
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {
            fieldname；fieldtype
            },
          }
         */
    }
    static create(record){
        let object=new routingModel();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    setRecord(record){
        this.record=record;
        this._setPointer();
        if(Object.keys(this.record.web).length===0){
            this.record.web=routingModel.webTpl();
            this.record.hier=routingModel.hierTpl();
        }

    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    /*async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(routingModel.tpl(),record);
        let result = await util.api.RoutingModel.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }*/
    /**
     * 新增模板
     * @returns {{features: *[], comments: string, web: {}, values: {}, className: string, published: string, type: string, version: string, hier: *[], others: {}}}
     */
    static tpl(){
        return {
            // id: "",
            type:"",
            className:"",
            version: "1.0",
            published: true,
            features:[],
            values:{
                "general": [
                    {propertyName:"name", propertyType:"String",defaultValue:"",filter:"false", comments:""},
                ]
            },
            hier:[],
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }

    /*******************************************************************************
     * hier,web编辑功能
     ******************************************************************************/
    /*webToHier(){
        let hier=this.record.hier[0];
        let web=this.record.web.data;
        hier.start=web.find(item=>item.shapeType==='routingStart').properties;
        hier.block=web.filter(shape=>shape.shapeType==='routingBlock').map(item=>{
            return item.properties;
        });
        hier.end=web.find(item=>item.shapeType==='routingEnd').properties;
        this.record.hier[0]=hier;
    }
    hierToWeb(){
        let web=this.record.web.data;
        let hier=this.record.hier[0];
        web.map(shape=>{
            if(shape.shapeType==='routingStart'){
                shape.properties=hier.start;
            }
            if(shape.shapeType==='routingEnd'){
                shape.properties=hier.end;
            }
            if(shape.shapeType==='routingBlock'){
                let block=hier.block.find(item=>item.id===shape.id);
                shape.properties=block;
            }
        });
        this.record.web.data=web;
    }*/
    getHierWCTpl(){
        return routingModel.hierWCTpl()
    }
    /**
     * 流程配置模板
     */
    static hierTpl(){
        return [
            routingModel.hierStartTpl(),
            routingModel.hierWCTpl(),
            routingModel.hierEndTpl()
        ]
    }
    static hierStartTpl(){
        return {
            id: "_start",
            //non-start/start/hangup/close，状态表明是否发起（start）、挂起为去除触发器（hangup）、关闭为流程执行完结（close）
            parentId:'root',
            type:"start",
            name:'routing',
            init:{},
            interfaceTemplate:{},
            interface:{}
        }
    }
    static hierWCTpl(){
        return {
            id: "_0",
            parentId:["_start"],
            type:"workCenter",
            name: "workCenter",
            init:{},
            finish:{},
            twins:{},
            template:{},
            instance:{}
        }
    }
    static hierEndTpl(){
        return {
            id:'_end',
            parentId:['_0'],
            type:'end',
            name:'end',
            finish:{},
            interfaceTemplate:{},
            interface:{}
        }
    }
    static webTpl(){
        return {data:[
            routingModel.webStartTpl(),
            routingModel.webWCTpl(),
            routingModel.webEndTpl(),
            ...routingModel.webLineTpl()
        ]}
    }
    static webStartTpl(){
        return {
            id: "_start",
            type: "routingStart",
            shapeType: "routingStart",
            title: "Start",
            properties: routingModel.hierStartTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 0,
        }
    }
    static webWCTpl(){
        return {
            id: "_0",
            type: "workCenter",
            shapeType: "workCenter",
            title: "workCenter",
            properties: routingModel.hierWCTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 170,
        }
    }
    static webEndTpl(){
        return {
            id: "_end",
            type: "routingEnd",
            shapeType: "routingEnd",
            title: "End",
            properties: routingModel.hierEndTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 360,
        }
    }
    static webLineTpl(){
        return [
            {
                id: "line1",
                type: "line",
                connectType: "elbow",
                cornersRadius: 0,
                customGap: 0,
                forwardArrow: "filled",
                from: "_start",
                to: "_0",
                fromSide: "bottom",
                toSide: "top",
                points: [
                    {
                        x: 500,
                        y: 80
                    },
                    {
                        x: 500,
                        y: 170
                    }
                ],
                width: 0,
                height: 90,
                stroke: "#2196F3",
                strokeWidth: 2,
                x: 500,
                y: 80
            },
            {
                id: "line2",
                type: "line",
                connectType: "elbow",
                cornersRadius: 0,
                forwardArrow: "filled",
                from: "_0",
                to: "_end",
                fromSide: "bottom",
                toSide: "top",
                points: [
                    {
                        x: 500,
                        y: 250
                    },
                    {
                        x: 500,
                        y: 360
                    }
                ],
                stroke: "#2196F3",
                strokeWidth: 2,
                height: 110,
                width: 0,
                x: 500,
                y: 250,
            }
        ]
    }
}
